package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.user.UserCreateRequest;
import com.example.fireassistant_server.dto.user.UserDto;
import com.example.fireassistant_server.dto.wechat.WechatUserCreateRequest;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.enums.Gender;
import com.example.fireassistant_server.exception.BizException;
import com.example.fireassistant_server.mapper.UserMapper;
import com.example.fireassistant_server.mapper.WechatUserMapper;
import com.example.fireassistant_server.service.CompanyService;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import com.example.fireassistant_server.vo.UserVo;
import com.example.fireassistant_server.vo.WechatUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/wechatUser")
@CrossOrigin
public class WechatUserController {
    WechatUserService wechatUserService;

    UserService userService;
    WechatUserMapper wechatUserMapper;

    UserMapper userMapper;


    //搜索全部微信用户
    // 微信登录时已注册的直接跳到显示面，未注册的需先注册
    @GetMapping("/me")
    WechatUserVo me(@RequestHeader(value = "X-WX-OPENID") String openId) {
        //return wechatUserMapper.toVo(wechatUserService.getCurrentUser(openId));
        return wechatUserMapper.toVo(wechatUserService.getCurrentUser("123456"));
    }

    //申请添加管理员权限
    @GetMapping("/elevate")
    UserVo Elevate(@RequestHeader(value = "X-WX-OPENID") String openId) {
        //WechatUserVo wechatUserVo = wechatUserMapper.toVo(wechatUserService.getCurrentUser(openId));
        WechatUserVo wechatUserVo = wechatUserMapper.toVo(wechatUserService.getCurrentUser("123456"));
        String userId= wechatUserVo.getUser().getId();
        return userMapper.toVo(userService.elevate(userId)) ;
    }

    @RequestMapping(method = RequestMethod.POST)
    WechatUserVo Create(@RequestHeader(value = "X-WX-OPENID") String openId, @Validated @RequestBody UserCreateRequest userCreateRequest){
        return wechatUserMapper.toVo(wechatUserService.createWechatUser(openId, userCreateRequest));
    }


    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Autowired
    public void setWechatUserMapper(WechatUserMapper wechatUserMapper) {
        this.wechatUserMapper = wechatUserMapper;
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


}
